addEvent(window,'load',InicializarEvento,false)

var pause = false



function InicializarEvento()
{
   var ob = document.getElementsByTagName('li');
   var objchild;
   for(f=0;f<ob.length;f++)
   {
	   objchild = ob[f];
	   addEvent(objchild,'click',presionaEnlace,false);
   }
   var nowplaying = document.getElementById('now-playing');
   addEvent(nowplaying,'click',presionaControl,false);
   var search = document.getElementById('search');
  // alert(search.getAttribute('type'));
   addEvent(search,'keyup',search,false);
   
}

function search(e){
	 if (window.event)
	  {
	    window.event.returnValue=false;
	 //   var url=window.event.srcElement.getAttribute('href');
	  //  cargarHoroscopo(url);     
	  }
	  else
	    if (e)
	    {
	      e.preventDefault();
	  //    var url=e.target.getAttribute('href');
	  //    cargarHoroscopo(url);     
	    }
}

function presionaControl(e){
	if(window.event){
		window.event.returnValue = false;
		var id = window.event.srcElement.parentNode.getAttribute('class');
		cambiarControl(id);
	}
	else{
		if(e){
			e.preventDefault();
			var id = e.target.parentNode.getAttribute('class');
			cambiarControl(id);
		}
	}
}

function actualizarTiempo(audio){
	var played = document.getElementById("played");
	played.innerHTML = toHMS(audio.currentTime);
}

function cambiarControl(id){
	var controls = document.getElementById("controls");
	var player = document.getElementsByTagName('audio');
	player = player[0];
	if(id == 'playing'){
		player.pause();
		pause = true;
		controls.setAttribute('class','paused');
	}
	else{
		player.play();
		pause = false;
		controls.setAttribute('class','playing');
	}	
}

var toHMS = function(it){
	    if(isNaN(it)){
	        return "00:00:00";
	    }
	    var hours = parseInt(it / 3600);
	    var minutes = parseInt((it % 3600) / 60);
	    var seconds = parseInt((it % 3600) % 60);
	    return ((hours<10?"0"+hours:hours) + ":" + (minutes<10?"0"+minutes:minutes) + ":" + (seconds<10?"0"+seconds:seconds));
	}


function presionaEnlace(e){
	if(window.event){
		window.event.returnValue = false;
		var id = window.event.srcElement.parentNode.getAttribute('data-song-id');
		cargarCancion(id);
	}
	else{
		if(e){
			e.preventDefault();
			var id = e.target.parentNode.getAttribute('data-song-id');
			cargarCancion(id);
		}
	}
}

var conexion1;
function cargarCancion(id)
{
  if(id=='')
  {
    return;
  }
  conexion1=crearXMLHttpRequest();
  conexion1.onreadystatechange = procesarEventos;
  conexion1.open("GET", './php/consultadatos.php?id='+id, true);
  conexion1.send(null);
}

function procesarEventos()
{
	var player = document.getElementById("player");
	var nowplaying = document.getElementById("now-playing");
	var audioPlayer1 = document.getElementsByTagName("audio")[0];
	  if(conexion1.readyState == 4)
	  {
		   var xml =  conexion1.responseXML;
		   var id = xml.getElementsByTagName('id');
		   id = id[0].firstChild.nodeValue;
		   var nombre = xml.getElementsByTagName('nombre');
		   nombre = nombre[0].firstChild.nodeValue;
		   var artista = xml.getElementsByTagName('artista');
		   artista = artista[0].firstChild.nodeValue;
	//	   var time = "ontimeupdate=""document.getElementById('played').innerHTML = Math.floor(this.currentTime);"" ";
	//	   	   time = time + "document.getElementById('total').innerHTML = Math.floor(this.duration);""";
		   var total = "'total'"
		   player.innerHTML =  '<audio id="audio" preload controls="controls" ontimeupdate="actualizarTiempo(this); document.getElementById('+total+').innerHTML=toHMS(this.duration);" autoplay> <source src="./php/music.php?streamkey='+id+'" type="audio/mp3"> </audio>';
		   var playingtitle = '<span class="playing-title">'+nombre+'</span>';
		   var playingartist = '<span class="playing-artist"> by '+artista+'</span>';
		   nowplaying.innerHTML = playingtitle + playingartist; 
	  } 
	  else 
	  {
		   player.innerHTML = 'Cargando...';
	  }
}

//***************************************
//Funciones comunes a todos los problemas
//***************************************
function addEvent(elemento,nomevento,funcion,captura)
{
if (elemento.attachEvent)
{
  elemento.attachEvent('on'+nomevento,funcion);
  return true;
}
else  
  if (elemento.addEventListener)
  {
    elemento.addEventListener(nomevento,funcion,captura);
    return true;
  }
  else
    return false;
}

function crearXMLHttpRequest() 
{
var xmlHttp=null;
if (window.ActiveXObject) 
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
else 
  if (window.XMLHttpRequest) 
    xmlHttp = new XMLHttpRequest();
return xmlHttp;
}